#include <gtk/gtk.h>

// 滚动事件处理函数
static void on_scroll(GtkEventControllerScroll *scroll, gdouble dx, gdouble dy, gpointer user_data) 
{
    g_print("scroll:%p, dx:%lf, dy:%lf", scroll, dx, dy); //注意部分鼠标驱动可能不识别滚轮
    GdkDevice *device = gdk_seat_get_pointer(gdk_display_get_default_seat(gdk_display_get_default()));
    GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(user_data));
    GdkModifierType state;
    auto ret = gdk_surface_get_device_position(surface, device, &dx, &dy, &state);
    if(ret)
    {
        g_print(" x:%lf, y:%lf", dx, dy);
    }
    putchar('\n');

}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWidget *window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "GtkEventControllerScroll 示例");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

    // 创建一个按钮作为示例小部件
    GtkWidget *button = gtk_button_new_with_label("滚动我");

    // 创建 GtkEventControllerScroll 控制器
    //GtkEventController *controller = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_DISCRETE);
    GtkEventController *controller = gtk_event_controller_scroll_new(GtkEventControllerScrollFlags(GTK_EVENT_CONTROLLER_SCROLL_DISCRETE|GTK_EVENT_CONTROLLER_SCROLL_VERTICAL));

    // 连接滚动事件到回调函数
    g_signal_connect(controller, "scroll", G_CALLBACK(on_scroll), window);

    // 将控制器添加到按钮中
    gtk_widget_add_controller(button, controller);
    //gtk_widget_add_controller(window, controller);

    // 设置窗口的内容为按钮
    gtk_window_set_child(GTK_WINDOW(window), button);

    // 显示所有内容
    gtk_widget_set_visible(window, true);
}

int main(int argc, char **argv) {
    GtkApplication *app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    int status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return status;
}
